_realname=libzen
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.4.41
pkgrel=2
pkgdesc="A C++ utility library (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://mediaarea.net"
license=("spdx:Zlib")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-cmake"
)
depends=(
  "${MINGW_PACKAGE_PREFIX}-gcc-libs"
)

source=("https://mediaarea.net/download/source/${_realname}/${pkgver}/${_realname}_${pkgver}.tar.bz2"
        001-allow-shared-libs.patch
        002-fix-pkgconfig.patch)
sha256sums=('eb237d7d3dca6dc6ba068719420a27de0934a783ccaeb2867562b35af3901e2d'
            '078274f6638007db51cc3415c8ae5c87b82d8cb166233c3c462ca126b8569a7a'
            '7c97a0bdd1fb99ee4386d296eed4fb6b71efec25e46e8f8902a1e9d246038e49')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd "${srcdir}/ZenLib"

  apply_patch_with_msg \
    001-allow-shared-libs.patch \
    002-fix-pkgconfig.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;" \
  ${MINGW_PREFIX}/bin/cmake \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -GNinja \
    "${_extra_config[@]}" \
    -DBUILD_SHARED_LIBS="ON" \
    -DBIN_INSTALL_DIR="bin" \
    -DLIB_INSTALL_DIR="lib" \
    -DINCLUDE_INSTALL_DIR:PATH="include" \
    ../ZenLib/Project/CMake

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  install -vDm 644 "${srcdir}/Zenlib/License.txt" -t "${pkgdir}${MINGW_PREFIX}/share/licenses/$_realname/"
  install -vDm 644 "${srcdir}/Zenlib/"{History.txt,README.md} -t "${pkgdir}${MINGW_PREFIX}/share/doc/$_realname/"
}
